<?php
class Schedule_model extends CI_Model{
    
    public $tablename = 'hpg_schedule';
    
    public $project_follows_tab = 'hpg_project_follows';
    
    public function __construct()
    {
        parent::__construct();
    }
    
    public function getList( $uid, $project_id, $date ){
        $querys = [];
        if( $date ){
            $lendate = strlen($date);
            if( $lendate == 6 ){
                //month
                $date .= '01';
                $stime = strtotime($date);
                $etime = strtotime(date('Ymd', $stime).'+1 month');
            } else if( $lendate == 8 ){
                //day
                $stime = strtotime($date);
                $etime = strtotime(date('Ymd', $stime).'+1 day');
            } else {
                //today
                $stime = strtotime(date('Ymd'));
                $etime = strtotime(date('Ymd', $stime).'+1 day');
            }
            $querys[] = '((start>='.$stime.' and start<='.$etime.') or (end>='.$stime.' and end<='.$etime.') or (`start`<='.$stime.' and `end`>='.$etime.'))';
        }
        if( $project_id ){
            $querys[] = 'b.project_id='.$project_id;
        }
        if( $uid ){
            $querys[] = 'a.uid='.$uid;
        }
        $wherestr = implode(' AND ', $querys);
        $list = $this->db->select('b.id,b.title,b.project_id,b.start,b.end,b.location,b.description,a.color,a.color_type')->from($this->tablename.' as b')
        ->join($this->project_follows_tab.' as a', 'b.project_id='.'a.project_id')
        ->where($wherestr)
        ->get_array();
        /* echo $this->db->select('b.id,b.title,b.project_id,b.start,b.end,b.location,b.description,a.color,a.color_type')->from($this->tablename.' as b')
        ->join($this->project_follows_tab.' as a', 'b.project_id='.'a.project_id')
        ->where($wherestr)->_compile_select();exit; */
        //->_compile_select();exit;
        return $list ? $list : [];
    }
    
    public function get( $id ){
        if( !$id ){
            return false;
        }
        $data = $this->db->select('*')->from($this->tablename)->where('id', $id)->fetchOne();
        return $data;
    }
}